﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using SD.Entities;
using SD.IBUS;
using SD.BUS;
using System.Data.SqlClient;

namespace SD.UI
{
    public partial class ShowTrustRegionList : BasePage
    {
        private static string ASCENDING = " ASC";
        private static string DESCENDING = " DESC";

        private bool includeInactive = false;
        private string filterExpression = "%";
        private string sortExpression = "Name";
        private string direction = ASCENDING;
        private int pageIndex = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
                {
                    includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
                    chkInActive.Checked = includeInactive;
                }

                if (!String.IsNullOrEmpty(Request.QueryString["filterExpression"]))
                {
                    filterExpression = Request.QueryString["filterExpression"].ToString();
                }

                if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
                {
                    sortExpression = Request.QueryString["sortExpression"].ToString();
                }

                if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
                {
                    direction = Request.QueryString["direction"].ToString();
                }

                if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
                {
                    gvTrustRegionList.PageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
                }

                BindData(includeInactive, filterExpression, sortExpression, direction); 
            }
        }

        private void BindData(bool includeInactive, string filterExpression, string sortExpression, string direction)
        {
            DataTable dt = LoadData(includeInactive, filterExpression);

            if (dt.Rows.Count > 0)
            {
                DataView dv = new DataView(dt);
                dv.Sort = sortExpression + direction;

                gvTrustRegionList.DataSource = dv;
                gvTrustRegionList.DataBind();
                lblMessage.Text = string.Empty;
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                gvTrustRegionList.DataSource = dt;
                gvTrustRegionList.DataBind();
                gvTrustRegionList.Rows[0].Visible = false;
                lblMessage.Text = "No records found";
            }

        }

        private DataTable LoadData(bool includeInactive, string filterExpression)
        {
            ITrustRegionBIZ trBIZ = new TrustRegionBIZ();
            DataTable dt = new DataTable();

            try
            {
                dt.Columns.Add("TrustRegionID");
                dt.Columns.Add("Name");
                dt.Columns.Add("Description");
                dt.Columns.Add("Country");
                dt.Columns.Add("IsActive");

                List<TrustRegion> list = trBIZ.GetByFilter(includeInactive, filterExpression);

                if (list.Count > 0)
                {
                    foreach (TrustRegion item in list)
                    {
                        DataRow row = dt.NewRow();
                        row["TrustRegionID"] = item.TrustRegionID;
                        row["Name"] = item.Name;
                        row["Description"] = item.Description;
                        row["Country"] = item.Country.Name;
                        row["IsActive"] = item.IsActive.ToString();

                        dt.Rows.Add(row);
                    }
                }
            }
            catch (SqlException ex)
            {
                ShowMessage(ERROR_SQL_EXCEPTION);
            }            
            catch (Exception ex)
            {
                Response.Redirect("CommomError.aspx");
            }

            return dt;
        }

        protected void lbnAll_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }


            filterExpression = "%";
            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void lbn09_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }

            filterExpression = "[0-9]%";
            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void lbnABCDE_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }

            filterExpression = "[A-E]%";
            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void lbnFGHIJ_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }

            filterExpression = "[F-J]%";
            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void lbnKLMN_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }

            filterExpression = "[K-N]%";
            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void lbnOPQR_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }

            filterExpression = "[O-R]%";
            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void lbnSTUV_Click(object sender, EventArgs e)
        {

            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }

            filterExpression = "[S-V]%";
            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void lbnWXYZ_Click(object sender, EventArgs e)
        {

            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }

            filterExpression = "[W-Z]%";
            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void chkInActive_CheckedChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["filterExpression"]))
            {
                filterExpression = Request.QueryString["filterExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }

            includeInactive = chkInActive.Checked;
            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {

        }

        protected void gvTrustRegionList_Sorting(object sender, GridViewSortEventArgs e)
        {
            sortExpression = e.SortExpression;

            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
                chkInActive.Checked = includeInactive;
            }

            if (!String.IsNullOrEmpty(Request.QueryString["filterExpression"]))
            {
                filterExpression = Request.QueryString["filterExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            if (direction == ASCENDING)
            {
                direction = DESCENDING;
            }
            else
            {
                direction = ASCENDING;
            }

            if (!String.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                pageIndex = Int32.Parse(Request.QueryString["pageIndex"].ToString());
            }

            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, pageIndex));
        }

        protected void gvTrustRegionList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["includeInactive"]))
            {
                includeInactive = bool.Parse(Request.QueryString["includeInactive"].ToString());
                chkInActive.Checked = includeInactive;
            }

            if (!String.IsNullOrEmpty(Request.QueryString["filterExpression"]))
            {
                filterExpression = Request.QueryString["filterExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["sortExpression"]))
            {
                sortExpression = Request.QueryString["sortExpression"].ToString();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["direction"]))
            {
                direction = Request.QueryString["direction"].ToString();
            }

            Response.Redirect(TrustRegionUrl(includeInactive, filterExpression, sortExpression, direction, e.NewPageIndex));
        }

        protected void gvTrustRegionList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private string TrustRegionUrl(bool includeInactive, string filterExpression, string sortExpression, string direction, int pageIndex)
        {
            return "ShowTrustRegionList.aspx?includeInactive=" + includeInactive + "&filterExpression="
                 + filterExpression + "&sortExpression=" + sortExpression + "&direction=" + direction + "&pageIndex=" + pageIndex;
        }
    }
}
